﻿/*
*	全国三级城市联动 js版
*/



function seChange(v,data){


	var ar = data;
	var str="0";
	for(i=0;i<v;i++){
		str+=("_"+(document.getElementById(s[i]).selectedIndex-1));
	};
	//console.log('str----->'+str);
	str = str.split('_');
	
	var ss=document.getElementById(s[v]);
	with(ss){
	
		length = 0;
		//console.log(v)
		options[0]=new Option(opt0[v],'');
		if(v==0){
			//if(dsy.Exists(str)){
				
				for(i=0;i<ar.length;i++){
					options[length]=new Option(ar[i].name,ar[i].code);
				}//end for
				if(v){ options[0].selected = true; }
			//}
		}//end if v
		
		if(v==1){
			
			
				
				//console.log('str[v]--->'+str[v]);
				//console.log(ar[str[v]]);
				
				if(ar[str[v]]){
					for(i=0;i<ar[str[v]].subRegion.length;i++){
							
							options[length]=new Option(ar[str[v]].subRegion[i].name,ar[str[v]].subRegion[i].code);
						
						}
				}
				
			
			}
/*		if(v==2){
				
				if(ar[str[v-1]]){
				for(i=0;i<ar[str[v-1]].subRegion[str[v]].subRegion.length;i++){
						
						options[length]=new Option(ar[str[v-1]].subRegion[str[v]].subRegion[i].name,ar[str[v-1]].subRegion[str[v]].subRegion.code);
					
					}
				}
				else{
						
						console.log('找不到')
					
					}
				
			
			}*/
		
		//console.log(s.length);
		if(++v<s.length){
			//console.log('if运行！'+v);
			seChange(v,data);}
	}//End with
}






var s=["s_province","s_city"];//三个select的name
var opt0 = ["请选择省","请选择市"];//初始值

function _init_area(){  //初始化函数
	
	//请求城市列表数据
	 $.ajax({
		 
		 	url:"/lt/city/list",
			type:"GET",
			success:function(data){
				
					//console.log(data.data);
					var data2 = data.data;
					for(i=0;i<s.length-1;i++){
					  //document.getElementById(s[i]).onchange=new Function("seChange("+(i+1)+","+data2+")");
							 (function (i){
							  document.getElementById(s[i]).onchange= function (){
									
										seChange(i+1,data2);
								  
								  }
							})(i);
					}
					seChange(0,data2)
				
				}
		 
		 });
	

}

